package com.dietmanager.view;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import com.dietmanager.model.Food;
import com.dietmanager.model.FoodModel;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * 
 * @author Danijel, Petar
 * @description AddFoodController is used as a separate window which user uses
 *              to control adding, creating and editing food.
 *
 */

public class AddFoodView extends Observable {

    ArrayList<Observer> observerList = new ArrayList<Observer>();

	// variables and objects
	public Button add;
	public TextField caloriesTextField, proteinsTextField,
			carbohydratesTextField, fatTextField, nameTextField;
	public VBox addFoodView;


	public AddFoodView() {

		createAddFoodView();

	}

	public void createAddFoodView() {
		addFoodView = new VBox();
		addFoodView.getStyleClass().add("addFoodHBox");
		addFoodView.setSpacing(10);
		addFoodView.setAlignment(Pos.CENTER);

		// TextFields
		caloriesTextField = new TextField();
		caloriesTextField.setMaxWidth(200);
		caloriesTextField.setPromptText("Enter Calories");
		nameTextField = new TextField();
		nameTextField.setMaxWidth(200);
		nameTextField.setPromptText("Enter the Name");
		fatTextField = new TextField();
		fatTextField.setMaxWidth(200);
		fatTextField.setPromptText("Enter Fat");
		carbohydratesTextField = new TextField();
		carbohydratesTextField.setMaxWidth(200);
		carbohydratesTextField.setPromptText("Enter Carbohydrates");
		proteinsTextField = new TextField();
		proteinsTextField.setMaxWidth(200);
		proteinsTextField.setPromptText("Enter Proteins");

		// Labels
		Label caloriesLabel = new Label("Calories");
		caloriesLabel.getStyleClass().add("textFieldLabel");
		Label nameLabel = new Label("Name");
		nameLabel.getStyleClass().add("textFieldLabel");
		Label fatLabel = new Label("Fat");
		fatLabel.getStyleClass().add("textFieldLabel");
		Label carbohydratesLabel = new Label("Carbohydrates");
		carbohydratesLabel.getStyleClass().add("textFieldLabel");
		Label proteinsLabel = new Label("Proteins");
		proteinsLabel.getStyleClass().add("textFieldLabel");

		add = new Button("Add Food");
		add.getStyleClass().add("customButton");
		add.setOnAction(new EventHandler<ActionEvent>() {

			// When add button pressed...
			@Override
			public void handle(ActionEvent event) {
				System.out.println(nameTextField.getText().toString() + caloriesTextField.getText() + carbohydratesTextField.getText() + proteinsTextField.getText());
				Food f = new Food(nameTextField.getText().toString(), 1);
				f.setCalories(Integer.parseInt(caloriesTextField.getText()));
				f.setFat(Integer.parseInt(fatTextField.getText()));				
				f.setCarbohydrates(Integer.parseInt(carbohydratesTextField.getText()));	
				f.setProtein(Integer.parseInt(proteinsTextField.getText()));
				notifyObservers(f);
			}

		}); // end of add button action

		// Add everything to VBox
		addFoodView.getChildren().addAll(caloriesLabel, caloriesTextField,
				proteinsLabel, proteinsTextField, carbohydratesLabel,
				carbohydratesTextField, fatLabel, fatTextField, nameLabel,
				nameTextField, add);

		return;
	}

	public VBox getAddFoodView() {
		return addFoodView;
	}
	
	public void addObserver(Observer o)
	{
		observerList.add(o);
	}
	
	public void addObserverCollection(ArrayList<Observer> obs)
	{
		observerList.clear();
		observerList = obs;
	}
	
	public void notifyObservers(Object o) {
		for(Observer ob : observerList)
			ob.update(this, o);
	}

}